package com.ruoyi.common.utils.baidu;

import org.noear.snack.ONode;

import java.util.HashMap;
import java.util.Map;

public class BaiduTransUtil {
    private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";

    private static final String appid = "20240913002149026";
    private static final String securityKey = "NC9tSGgxXJPf78l7kWst";



    public static String getTransResult(String query) {
        long startTime = 0L;
        long endTime = 0L;
        startTime = System.currentTimeMillis();
        Map<String, String> params = buildParams(query, "zh", "en");
        String strJson= HttpGet.get(TRANS_API_HOST, params);

        ONode oNode = ONode.load(strJson);
        ONode node = oNode.select("$.trans_result[0].dst");
        endTime = System.currentTimeMillis();

        long sBuffer_timer_delta = (endTime - startTime) / 1000;
        System.out.println("百度翻译时间:" + (sBuffer_timer_delta)+"秒");
        return  node.toString();

    }

    private static Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);

        params.put("appid", appid);

        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);

        // 签名
        String src = appid + query + salt + securityKey; // 加密前的原文
        params.put("sign", MD5.md5(src));

        return params;
    }

    public static void main(String[] args) {
       String query = "视频中是一位年轻男子，他穿着一件白色T恤和浅色裤子，戴着眼镜，脖子上挂着一条项链。他站在一栋现代建筑前，背景中可以看到一辆自行车和一些建筑物。男子先是抽烟，然后接电话，最后挂断电话。";
      String str =   getTransResult(query);
        System.out.println(str);
        }

}
